﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PC_Touch.DataOP
{
    public class CollectionManagement
    {
        public int id { get; set; }
        public string createDataTime { get; set; }
        public string parameterName { get; set; }
        public string parameterAddress { get; set; }
        public string parameterType { get; set; }
        public int parameterLength { get; set; }
        public double factor { get; set; }
        public string unit { get; set; }
        public string eventName { get; set; }
        public int eventId { get; set; }
        public string triggerAddress { get; set; }


    }
    public class CollectionValue
    {
        public int id { get; set; }
        public string createDataTime { get; set; }
        public string parameterName { get; set; }
        public string parameterValue { get; set; }
        public string unit { get; set; }
        public string triggerValue { get; set; }
    }
    public class EventSelectItem
    {
        public int eventId { get; set; }
        public string eventName { get; set; }

    }
    public class CollectionManagementOP
    {
        private readonly string _connectionString;
        public CollectionManagementOP(string connectionString)
        {
            _connectionString = connectionString;
        }
        public void AddParameterToDatabase(CollectionManagement p)
        {
            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (var command = new SQLiteCommand($"INSERT INTO CollectionManagement (parameterName, parameterAddress,parameterType,parameterLength,factor,unit,eventId) VALUES ('{p.parameterName}','{p.parameterAddress}','{p.parameterType}',{p.parameterLength},{p.factor},'{p.unit}','{p.eventId}')", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }

        public void UpdateParameterInDatabase(CollectionManagement p)
        {
            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (var command = new SQLiteCommand($"UPDATE CollectionManagement SET parameterName = '{p.parameterName}', parameterAddress = '{p.parameterAddress}',parameterType='{p.parameterType}',parameterLength={p.parameterLength},factor={p.factor},unit='{p.unit}',eventId='{p.eventId}' WHERE id = {p.id}", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
        public void DeleteParameterFromDatabase(CollectionManagement p)
        {
            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (var command = new SQLiteCommand($"DELETE FROM CollectionManagement WHERE id = {p.id}", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
    }
}
